package com.pf.core.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

	public static List<File> getFileList(String path){
       List<File> files=new ArrayList<File>();
       getFileList(files,path,null);
	   return files;
	}

	public static List<File> getFileList(String path,String... types){
       List<File> files=new ArrayList<File>();
       getFileList(files,path,types);
	   return files;
	}

	private static void getFileList(List<File> list,String path,String... types){
         File file=new File(path);
         if(file.isFile()){
        	 if(types!=null && types.length>0){
        		 String fileName=file.getName();
        		 int index=fileName.lastIndexOf(".");
        		 if(index>0){
        			 for(String t : types){
        				 if(t.equalsIgnoreCase(fileName.substring(index+1))){
            				 list.add(file);
            				 break;
            			 }
        			 }
        		 }
        	}else{
        		list.add(file);
        	}
         }else {
        	 File[] files=file.listFiles();
        	 for(File child : files){
        		 getFileList(list,child.getPath(),types);
        	 }
         }
	}

	public static void copy(File file ,String path) throws Exception{
		InputStream dis=new FileInputStream(file);
		OutputStream ops=new FileOutputStream(path+File.separator+file.getName());
		try{
			byte[] buffer=new byte[1024];
			int i=dis.read(buffer);
			while(i!=-1){
				ops.write(buffer,0,i);
				i=dis.read();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			ops.flush();
			ops.close();
			dis.close();
		}

	}


	public static void main(String[] args){
		List<File> list=getFileList("C:\\Documents and Settings\\Administrator\\.m2\\repository","jar");
		for(File file : list){
			System.out.println(file.getName());
		}
		for(File file : list){
			try {
				copy(file,"E:\\taobao_lib");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
